#usr/bin/perl
use strict;
use utf8;
use Data::Dumper; 

$Data::Dumper::Useqq = 1;
{
 no warnings 'redefine';
 sub Data::Dumper::qquote
 {
   my $s = shift;
   return "'$s'";
	
 }

}

binmode(STDOUT, ":encoding(utf-8)");
binmode(STDERR, ":encoding(utf-8)");

open (FILE, "out.txt") or die "Sorry, but i can't open 'out.txt'";
binmode(FILE, ":encoding(utf-8)");
my @s=<FILE>;
my $q = 0;
open(my $f,'>','out.gv') or die "Sorry, but i can't open 'out.gv'";
binmode($f, ":encoding(utf-8)");

print $f "digraph finite_state_machine {
	size=\"8\,5\";
	node [shape = ellipse];edge [];";


sub nrm{
	my ($b) = @_;
	$b = lc ($b);
	$b =~ s/ё/е/;

	return $b; 
}

my @facts; #keep facts
my %f1; 
my ($line, $i, $a);
my %persn; #keep Rel like "norm"-> {text,text...}

foreach $line (@s){
	if ($i = index($line, "FPerson") != -1)
	{	
		my %t = %f1;
		push @facts, \%t;
		%f1 = ();
	#	print Dumper (\@facts);
	
		$i = index ($line, "= ");
		$a = substr ($line, $i+2,length($line)-$i-3);
		$f1{"FPerson"}= $a; 
		push @{$persn{nrm($a)}}, $a;
		
	}

	if ($i = index($line, "SPerson") != -1)
        {
                $i = index ($line, "= ");
                $a = substr ($line, $i+2, length($line)-$i-3);
                $f1{SPerson}= $a; 
		push @{$persn{nrm ($a)}},$a;
        }
	
	if ($i = index($line, "Rel") != -1)
        {
		$i = index ($line, "= ");
                $a = substr ($line, $i+2, length($line)-$i-3);
                $f1{Rel}=$a;
		             	         			
	}

	if ($i = index($line, "ExRel") != -1)
        {
                $i = index ($line, "= ");
                $a = substr ($line, $i+2, length($line)-$i-3);
                $f1{Rel}=$a;
		    
		        
}
}
	
my %seen;
my @per; #keep norm names
my $i;
my $j=0;
my $g=0;
my %norm; #keep norm only with multiple choice of texts
my %change; #keep Rel like text -> norm for only those, who changed it's norm

foreach $i (keys %persn){
 	push @per, $i;
	@{$persn{$i}} = grep { ! $seen{$_}++ } @{$persn{$i}};
}

for ($j; $j <= scalar (@per); $j++){
	for ($g; $g <= scalar (@per); $g++){
		if ($j ne $g) {
			if (@per[$j]=~ /\Q@per[$g]\E/ ) {push @{$norm{@per[$g]}}, @per[$j];}		
			 

		 }
	}	
$g=0;
}

foreach my $k (keys  %norm){
	my @v = @{$norm{$k}};
	if (scalar (@v) eq 1) {
		push @{$persn{@v[0]}}, @{$persn{$k}} ;
		$change{@{$persn{$k}}[0]} = $persn{@v[0]}[0];
		delete ($persn{$k});
	}
}

my %convNorm; #keep Rel like text-> norm;
my %num; # keep Rel like element -> pos in massive;

foreach my $n (keys %persn){
	my $line = "";
 	my $count = 0;
	foreach my $l (@{$persn{$n}}){
		$line = $line.$l." | ";
		$count++;
		$convNorm{$l} = $n;
		$num{$l} = $count;  
	}
	$line = substr ($line, 0, -2);	
	print $f "\"$n\"\[\nlabel = \"$line\"\nshape = \"ellipse\"\n\];\n";
}

my $count =0; 
my $t;
my %t;

foreach my $k (@facts){
	$t = ($k)->{'Rel'};
	print  $f "\"$convNorm{($k)->{ 'FPerson' }}\":f$num{($k)->{'FPerson'}} ->\"$convNorm{($k)->{'SPerson'}}\":f$num{($k)->{'SPerson'}}\[\nid = $count\nlabel = \"$t\"\n\];\n "; 	
	$count++;
} 

print $f "}";

#print Dumper(\@facts);

